package com.strange.mdl.module.mdl.controller.admin.sutras;

import com.strange.mdl.framework.apilog.core.annotation.ApiAccessLog;
import com.strange.mdl.framework.common.pojo.CommonResult;
import com.strange.mdl.framework.common.pojo.PageParam;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;
import com.strange.mdl.framework.excel.core.util.ExcelUtils;
import com.strange.mdl.module.mdl.controller.admin.sutras.vo.volumes.MdlSutraVolumesPageReqVO;
import com.strange.mdl.module.mdl.controller.admin.sutras.vo.volumes.MdlSutraVolumesRespVO;
import com.strange.mdl.module.mdl.controller.admin.sutras.vo.volumes.MdlSutraVolumesSaveReqVO;
import com.strange.mdl.module.mdl.dal.dataobject.sutras.MdlSutraVolumesDO;
import com.strange.mdl.module.mdl.service.sutras.MdlSutraVolumesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.strange.mdl.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.strange.mdl.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 经书卷")
@RestController
@RequestMapping("/mdl/sutra-volumes")
@Validated
public class MdlSutraVolumesController {

    @Resource
    private MdlSutraVolumesService sutraVolumesService;

    @PostMapping("/create")
    @Operation(summary = "创建经书卷")
    @PreAuthorize("@ss.hasPermission('mdl:sutra-volumes:create')")
    public CommonResult<Long> createSutraVolumes(@Valid @RequestBody MdlSutraVolumesSaveReqVO createReqVO) {
        return success(sutraVolumesService.createSutraVolumes(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新经书卷")
    @PreAuthorize("@ss.hasPermission('mdl:sutra-volumes:update')")
    public CommonResult<Boolean> updateSutraVolumes(@Valid @RequestBody MdlSutraVolumesSaveReqVO updateReqVO) {
        sutraVolumesService.updateSutraVolumes(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除经书卷")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdl:sutra-volumes:delete')")
    public CommonResult<Boolean> deleteSutraVolumes(@RequestParam("id") Long id) {
        sutraVolumesService.deleteSutraVolumes(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除经书卷")
    @PreAuthorize("@ss.hasPermission('mdl:sutra-volumes:delete')")
    public CommonResult<Boolean> deleteSutraVolumesList(@RequestParam("ids") List<Long> ids) {
        sutraVolumesService.deleteSutraVolumesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得经书卷")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdl:sutra-volumes:query')")
    public CommonResult<MdlSutraVolumesRespVO> getSutraVolumes(@RequestParam("id") Long id) {
        MdlSutraVolumesDO sutraVolumes = sutraVolumesService.getSutraVolumes(id);
        return success(BeanUtils.toBean(sutraVolumes, MdlSutraVolumesRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "基于经书id获得经书卷分页")
    @Parameter(name = "sutraId", description = "经书编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdl:sutra-volumes:query')")
    public CommonResult<List<MdlSutraVolumesRespVO>> getSutraVolumesList(@RequestParam("sutraId") Long sutraId) {
        List<MdlSutraVolumesDO> list = sutraVolumesService.getSutraVolumesList(sutraId);
        return success(BeanUtils.toBean(list, MdlSutraVolumesRespVO.class));
    }


    @GetMapping("/page")
    @Operation(summary = "获得经书卷分页")
    @PreAuthorize("@ss.hasPermission('mdl:sutra-volumes:query')")
    public CommonResult<PageResult<MdlSutraVolumesRespVO>> getSutraVolumesPage(@Valid MdlSutraVolumesPageReqVO pageReqVO) {
        PageResult<MdlSutraVolumesDO> pageResult = sutraVolumesService.getSutraVolumesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MdlSutraVolumesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出经书卷 Excel")
    @PreAuthorize("@ss.hasPermission('mdl:sutra-volumes:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSutraVolumesExcel(@Valid MdlSutraVolumesPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MdlSutraVolumesDO> list = sutraVolumesService.getSutraVolumesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "经书卷.xls", "数据", MdlSutraVolumesRespVO.class,
                BeanUtils.toBean(list, MdlSutraVolumesRespVO.class));
    }

}
